package com.digitalxpert.jargle.sgtable;

import java.util.Hashtable;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class TableHelper {
    protected FlexibleTable table;
    private DefaultTableModel tableModel;
    private DefaultTableModel rowHeaderModel;
    private Object[] header;
    private TableSortHeader sorter;

    public TableHelper(FlexibleTable table, Object[] header, boolean isHeaderAction) {
        this.header = header;
        this.table = table;
        initTable();
    }

    private void initTable() {
        tableModel = new NoEditableTableModel(header, 0);
        rowHeaderModel = new RowHeaderTableModel();
        table.getRowHeader().setModel(rowHeaderModel);
        table.getRowHeader().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        if (sorter == null) {
            sorter = new TableSortHeader(tableModel);
        } else {
            sorter.setTableModel(tableModel);
        }
        table.setModel(sorter);
        sorter.setTableHeaderRenderer(table.getTableHeader());
        setMultylineColumn();
    }

    private void setMultylineColumn() {
        MultiLineCellRenderer rendererC = new MultiLineCellRenderer();
        TableColumnModel columnModel = table.getColumnModel();
        for (int i = 0; i < columnModel.getColumnCount(); i++) {
            TableColumn column = columnModel.getColumn(i);
            column.setCellRenderer(rendererC);
        }
    }

    public void setValueAt(int row, int column, Object value) {
        tableModel.setValueAt(value, row, column);
    }

    public void addRow(Object[] row) {
        tableModel.addRow(row);
        rowHeaderModel.addRow(new Object[] { "Line: " + 1 });
    }

    public void removeRow(int row) {
        tableModel.removeRow(row);
        rowHeaderModel.removeRow(row);
    }

    /*
     * public void removeRowAndUpdate(int row){ dtm.removeRow(row); update(); }
     */
    public Hashtable<Object, Object> getRow(int row) {
        Hashtable<Object, Object> out = new Hashtable<Object, Object>();
        int length = header.length;
        for (int i = 0; i < length; i++) {
            if (sorter.getValueAt(row, i) == null) {
                out.put(header[i], "");
            } else {
                out.put(header[i], sorter.getValueAt(row, i));
            }
        }
        return out;
    }

    public void clear() {
        initTable();
    }

    public DefaultTableModel getTableModel() {
        return tableModel;
    }

    public void setDefineSortingStatus(int column, int status) {
        sorter.setDefineSortingStatus(column, status);
    }

    public JTable getTable() {
        return table;
    }

    public void setColumnWidth(int column, int width) {
        TableColumnModel columnModel = table.getColumnModel();
        TableColumn col = columnModel.getColumn(column);
        col.setMinWidth(width);
    }

    public void setHiddenColumn(int column) {
        TableColumnModel columnModel = table.getColumnModel();
        TableColumn col = columnModel.getColumn(column);
        table.removeColumn(col);
    }

    public JScrollPane getScrollWithRowHeader() {
        JScrollPane scroll = new JScrollPane();
        scroll.getViewport().add(table);
        table.initRowHeader(scroll);
        return scroll;
    }
}
